package common

import "io"

type CountingReaderReadCallback func(totalReadLen, readLen int64)

type CountingReader struct {
	reader    io.Reader
	bytesRead int64
	f         CountingReaderReadCallback
}

func (r *CountingReader) Read(p []byte) (n int, err error) {
	n, err = r.reader.Read(p)
	r.bytesRead += int64(n)
	r.f(r.bytesRead, int64(n))
	return n, err
}

func NewCountingReader(r io.Reader, f CountingReaderReadCallback) *CountingReader {
	return &CountingReader{
		reader:    r,
		bytesRead: 0,
		f:         f,
	}
}
